/*
    msgBox 消息提示框
**/

  (function($){

	  	var MsgBox = function (element, options) {
		       this.init(element, options);
	     }

		MsgBox.prototype = {
		
			constructor: MsgBox,
			init: function (element, options) {			 
						  var $el = this.$element = $(element);		
						  this.options = $.extend({}, $.fn.mobileMsgBox.defaults, options);			
						  var id=$el.attr("id");
						  if(!id) id=randomMsgBoxId();
						  this.options.msgboxId=id;
						  var pagecontainer=$("#"+id).closest(".ui-page");
						  if (!pagecontainer.length)
						  {
							  pagecontainer=$("body");
						  }
						  var msgbox="";
						  //屏蔽多次触发同一个消息框
						  if( $("#prompt-msgbox-"+id).length>=1) return;
						  msgboxTemplate="<div class='row-fluid ui-mobile-prompt "+options.position+" alert  "+this.options.theme+"  in' id='prompt-msgbox-"+id+"'>"
						  if(this.options.title&&(this.options.boxType=='prompt'||this.options.boxType=='confirm')){
									var title="<h5>"+this.options.title+"</h5>";
									msgboxTemplate+=title; 
						 }
						 
						 if(this.options.msgText){
							    var bodyContent="";    
							    bodyContent+="<p>"
								 if(this.options.msgIcon)
								     bodyContent+="<i class='"+this.options.msgIcon+"'>"+"</i>";
								bodyContent+=this.options.msgText+"</p>";
								msgboxTemplate+=bodyContent;
						 }
						 if(this.options.boxType=='alert'){
								 var closeBtn="<div class='row-fluid'><div class='prompt'><a class='ui-mobile-prompt-close btn "+options.closeBtnStyle+"' >确定</a></div></div>";
								msgboxTemplate+=closeBtn;
								  
						  }
						  else if(this.options.boxType=='prompt'){
							 
								 var closeBtn="<div class='row-fluid'><div class='prompt'><a class='ui-mobile-prompt-close btn "+options.closeBtnStyle+"' >确定</a></div></div>";
								msgboxTemplate+=closeBtn;
								  
						  }
						  else if(this.options.boxType=='confirm'){
								 var closeBtn="<div class='row-fluid'><div class='prompt'><div class='ui-mobile-prompt-block-a'><a class='ui-mobile-prompt-ok  btn "+options.closeBtnStyle+"' >确定</a></div><div class='ui-mobile-prompt-block-a'><a class='ui-mobile-prompt-close block-a btn "+options.closeBtnStyle+"' >取消</a></div></div></div>";
								msgboxTemplate+=closeBtn;
								  
						  }
						 
						  msgboxTemplate+="</div>";
			
						  if(this.options.modal){
						
								 msgboxTemplate +="<div class='modal-backdrop  in' id='prompt-msgbox-modal-"+id+"'></div>";
								
						  }
						  pagecontainer.append($(msgboxTemplate));
							 //boxType-alert
						   $("#prompt-msgbox-"+id+" .ui-mobile-prompt-close").bind("click",function(){
										   $("#prompt-msgbox-"+id).alert('close');
										   $("#prompt-msgbox-modal-"+id).remove();
								   
							});
						   
						   
							var callback=this.options.callback;
						 
							 $("#prompt-msgbox-"+id+" .ui-mobile-prompt-ok").bind("click",function(){
										   $("#prompt-msgbox-"+id).alert('close');
										   $("#prompt-msgbox-modal-"+id).remove();
										  if(callback) 
											  callback();
										   //return true;
								   
							});
			
							 //计算消息框位置
						  var scrollTop=$(window).scrollTop();
						  var screenHeight=$(window).height();
						  if(this.options.position!='top'&&this.options.position!='bottom'&&this.options.position!='top left'&&this.options.position!='bottom left'
						  &&this.options.position!='top right'&&this.options.position!='bottom right'){
							   var promptHeight=$("#prompt-msgbox-"+id).height();
							   $("#prompt-msgbox-"+id).css({"top":screenHeight/2+scrollTop-promptHeight/2+"px"});
						  }
						 
						  
						 $("#prompt-msgbox-"+id).alert();
						  
						  if(this.options.delayTime){
						
							//延迟关闭消息框
							 setTimeout(function(){
								 $("#prompt-msgbox-"+id).alert('close');
								 $("#prompt-msgbox-modal-"+id).remove();
								
							 },this.options.delayTime);
						 }
		
			},
			open:function(element,options){
				var self=this;
				self.init(element,options);	
				
			},
			close:function(element,options){
				var self=this;
				 $("#prompt-msgbox-"+self.options.msgboxId).alert('close');
				 $("#prompt-msgbox-modal-"+self.options.msgboxId).remove();
	
			}

	};
       
     
	 var randomMsgBoxId=function(){
	 
			var date = new Date(); //日期对象
			var msgId = "";
			msgId = date.getFullYear()+""; //读英文就行了
			msgId = msgId + (date.getMonth()+1)+"";//取月的时候取的是当前月-1如果想取当前月+1就可以了
			msgId = msgId + date.getDate()+"";
			msgId = msgId + date.getHours()+"";
			msgId = msgId + date.getMinutes()+"";
			msgId = msgId + date.getSeconds()+"";
			return msgId;
		
	 
	 }

 /* mobileMsgBox PLUGIN DEFINITION
	* ======================== */

	var old = $.fn.mobileMsgBox

	$.fn.mobileMsgBox = function (option) {
		
			var $this = $(this)
				, data = $this.data('msgbox')
				, options = $.extend({}, $.fn.mobileMsgBox.defaults, $this.data(), typeof option == 'object' && option);
			$this.data('msgbox', (data = new MsgBox(this, options)));
			if(option=='open') data.open(this,options);
			if(option=='close') data.close(this,options);
			//else if(data) data.init(this,options);		
	}
	
	$.fn.mobileMsgBox.defaults = {
		    theme:'alert',  //提示框主题：info,warning,danger,success等
			msgIcon:"", //提示信息图标
			closeBtnStyle:'',  //提示框按钮样式
	        msgText:"",          //提示内容
			delayTime:null,       //提示框message类型 延迟关闭时间设置
			modal:false,        //是否启用遮罩
			position:'',        //定位
			boxType:"",//prompt,alert,confirm
			title:"提示信息"
	}

 /* mobileMsgBox NO CONFLICT
	* ================== */

	$.fn.mobileMsgBox.noConflict = function () {
		$.fn.mobileMsgBox = old;
		return this;
	}

})(jQuery);


     